import { storageLocal } from "@pureadmin/utils";
import { userKey } from "@/utils/auth";
import type { Directive, DirectiveBinding } from "vue";

export const permission: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding;
    if (value) {
      let userInfo = storageLocal().getItem(userKey);
      let have = value.filter(item => {
        return userInfo.roles.find(i => i === item);
      });
      if (!have.length) {
        el.parentNode?.removeChild(el);
      }
      // console.log(value, userInfo, 'userInfo')
      // !hasAuth(value) && el.parentNode?.removeChild(el);
    } else {
      throw new Error(
        "[Directive: auth]: need auths! Like v-auth=\"['btn.add','btn.edit']\""
      );
    }
  }
};
